-- _____ ______   ________  ________   ___  _________  ________  ________          ________  ________  ___  ___
--|\   _ \  _   \|\   __  \|\   ___  \|\  \|\___   ___\\   __  \|\   __  \        |\   ____\|\   __  \|\  \|\  \
--\ \  \\\__\ \  \ \  \|\  \ \  \\ \  \ \  \|___ \  \_\ \  \|\  \ \  \|\  \       \ \  \___|\ \  \|\  \ \  \\\  \
-- \ \  \\|__| \  \ \  \\\  \ \  \\ \  \ \  \   \ \  \ \ \  \\\  \ \   _  _\       \ \  \    \ \   ____\ \  \\\  \
--  \ \  \    \ \  \ \  \\\  \ \  \\ \  \ \  \   \ \  \ \ \  \\\  \ \  \\  \|       \ \  \____\ \  \___|\ \  \\\  \
--   \ \__\    \ \__\ \_______\ \__\\ \__\ \__\   \ \__\ \ \_______\ \__\\ _\        \ \_______\ \__\    \ \_______\
--    \|__|     \|__|\|_______|\|__| \|__|\|__|    \|__|  \|_______|\|__|\|__|        \|_______|\|__|     \|_______|
-- banner online tools
-- https://www.bootschool.net/ascii;bsid=FB5C2059677902AEBA0B2640589E9FE8

local require  = require;
local ngx      = ngx;
local debug    = debug;
local string   = string;
local tonumber = tonumber;
local setmetatable = setmetatable
local pairs    = pairs
local type     = type
local table    = table

local table_concat     = table.concat

local math       = math
local math_floor = math.floor

local uu         = require("modules.commhm.utils");
local log        = require("modules.commhm.log");
local m_util     = require("modules.commhm.monitor.MonitorUtil");

--- 特性:
--- 1.CPU核数
--- 2.CPU负载监控
--- 3.CPU使用率监控

local _M = { _VERSION = '0.43.1' }



---获取CPU核数
---@return number	CPU核数
function _M.getCores()
	local ret_string_ = uu.os_exec( "grep 'model name' /proc/cpuinfo | wc -l" )
	return tonumber(ret_string_)
end


---获取CPU负载
---@return number,number,number	最近1分钟、5分钟、15分钟的负载
function _M.getAverage()
	local ret_string_ = uu.os_exec( "uptime | awk -F'average:' '{print $2}' | awk '{printf $1$2$3}'" )
	local list_ = uu.split(ret_string_, ',')
	return list_[1], list_[2], list_[3]
end


local function get_cpu_usage(system_info)
	local cpu_user = m_util.string_match_num(system_info, "[0-9]+%.?[0-9]*%sus,")
	local cpu_system = m_util.string_match_num(system_info, "[0-9]+%.?[0-9]*%ssy,")
	local cpu_nice = m_util.string_match_num(system_info, "[0-9]+%.?[0-9]*%sni,")
	local cpu_idle = m_util.string_match_num(system_info, "[0-9]+%.?[0-9]*%sid,")
	local cpu_wait = m_util.string_match_num(system_info, "[0-9]+%.?[0-9]*%swa,")
	local cpu_hardware_interrupt = m_util.string_match_num(system_info, "[0-9]+%.?[0-9]*%shi,")
	local cpu_software_interrupt = m_util.string_match_num(system_info, "[0-9]+%.?[0-9]*%ssi,")
	local cpu_steal_time = m_util.string_match_num(system_info, "[0-9]+%.?[0-9]*%sst")

	local cpu_total = cpu_user + cpu_nice + cpu_system + cpu_wait + cpu_hardware_interrupt + cpu_software_interrupt + cpu_steal_time + cpu_idle
	local cpu_time = cpu_user + cpu_nice + cpu_system + cpu_wait + cpu_hardware_interrupt + cpu_software_interrupt + cpu_steal_time

	local cpu_usage = cpu_time / cpu_total
	return cpu_usage
end


---获取CPU使用率
---@return number	总的CPU使用率(0-1之间)
function _M.getUsage()
	local ret_string_ = uu.os_exec( "top -bn1 -p 1000" )  ---过滤日志
	local cpu_usage = get_cpu_usage(ret_string_)
	return cpu_usage
end


return _M;
